<template>
  <div class="matchmaker-list-container">
    <h2 class="page-title">红娘管理</h2>
    
    <!-- 工具栏 -->
    <el-card shadow="never" class="toolbar-card">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-button type="primary" icon="Plus" @click="$router.push('/matchmaker/create')">
            添加红娘
          </el-button>
          <el-button icon="Refresh" @click="loadList">刷新</el-button>
        </el-col>
        <el-col :span="18">
          <el-space wrap>
            <el-select v-model="filters.matchmakerType" placeholder="红娘类型" clearable style="width: 120px" @change="loadList">
              <el-option label="全部" :value="undefined" />
              <el-option label="兼职" :value="1" />
              <el-option label="全职" :value="2" />
            </el-select>
            
            <el-select v-model="filters.level" placeholder="等级" clearable style="width: 120px" @change="loadList">
              <el-option label="全部" :value="undefined" />
              <el-option label="青铜" :value="1" />
              <el-option label="白银" :value="2" />
              <el-option label="黄金" :value="3" />
              <el-option label="铂金" :value="4" />
              <el-option label="钻石" :value="5" />
            </el-select>
            
            <el-input v-model="filters.keyword" placeholder="搜索姓名/手机" clearable style="width: 200px" @keyup.enter="loadList">
              <template #append>
                <el-button icon="Search" @click="loadList" />
              </template>
            </el-input>
          </el-space>
        </el-col>
      </el-row>
    </el-card>
    
    <!-- 列表 -->
    <el-card shadow="never" class="table-card">
      <el-table v-loading="loading" :data="list" stripe>
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="avatarUrl" label="头像" width="80">
          <template #default="{ row }">
            <el-avatar :src="row.avatarUrl" />
          </template>
        </el-table-column>
        <el-table-column prop="realName" label="姓名" width="100" />
        <el-table-column prop="phone" label="手机号" width="130" />
        <el-table-column prop="matchmakerType" label="类型" width="80">
          <template #default="{ row }">
            <el-tag :type="row.matchmakerType === 2 ? 'success' : 'info'" size="small">
              {{ row.matchmakerType === 2 ? '全职' : '兼职' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="level" label="等级" width="80">
          <template #default="{ row }">
            <el-tag :type="getLevelColor(row.level)" size="small">
              {{ getLevelText(row.level) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="successCouples" label="成功对数" width="100" />
        <el-table-column prop="cityName" label="城市" width="120" />
        <el-table-column prop="status" label="状态" width="80">
          <template #default="{ row }">
            <el-tag :type="row.status === 1 ? 'success' : 'danger'" size="small">
              {{ row.status === 1 ? '正常' : row.status === 0 ? '禁用' : '离职' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" size="small" link @click="$router.push(`/matchmaker/edit/${row.matchmakerId}`)">
              编辑
            </el-button>
            <el-button type="danger" size="small" link @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="loadList"
          @current-change="loadList"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import request from '@/utils/request'
import { API_ENDPOINTS } from '@/config/api'

const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const list = ref([])

const filters = reactive({
  matchmakerType: null,
  level: null,
  keyword: ''
})

const loadList = async () => {
  loading.value = true
  try {
    const response = await request.post(API_ENDPOINTS.MATCHMAKER_LIST, {
      pageNum: currentPage.value,
      pageSize: pageSize.value,
      matchmakerType: filters.matchmakerType,
      level: filters.level,
      keyword: filters.keyword
    })
    if (response.code === 200) {
      list.value = response.data.records || response.data.list || response.data || []
      total.value = response.data.total || list.value.length
    }
  } catch (error) {
    console.error('加载失败:', error)
    ElMessage.error('加载失败')
  } finally {
    loading.value = false
  }
}

const getLevelText = (level) => {
  const texts = { 1: '青铜', 2: '白银', 3: '黄金', 4: '铂金', 5: '钻石' }
  return texts[level] || '未知'
}

const getLevelColor = (level) => {
  const colors = { 1: '', 2: 'info', 3: 'warning', 4: 'success', 5: 'danger' }
  return colors[level] || ''
}

const handleDelete = async (row) => {
  try {
    await ElMessageBox.confirm('确定要删除这个红娘吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    const response = await request.delete(`${API_ENDPOINTS.MATCHMAKER_DELETE}/${row.matchmakerId}`)
    if (response.code === 200) {
      ElMessage.success('删除成功')
      loadList()
    }
  } catch (error) {
    if (error !== 'cancel') console.error('删除失败:', error)
  }
}

onMounted(() => loadList())
</script>

<style scoped>
.matchmaker-list-container { padding: 0; }
.page-title { font-size: 24px; font-weight: bold; color: #333; margin: 0 0 20px 0; }
.toolbar-card { margin-bottom: 20px; }
.table-card { margin-top: 20px; }
.pagination-container { display: flex; justify-content: flex-end; margin-top: 20px; }
</style>

